<?php

class AdminQLThongBao extends BaseController {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
       return View::make('quantri.qltb')->with('title','Quản lý thông báo');
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
        return View::make('adminqlthongbaos.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
        return View::make('adminqlthongbaos.show');
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
        return View::make('adminqlthongbaos.edit');
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
	public function lay_thong_tin_bo_mon1()
	{
		$array = DB::table('bo_mon')
			->select('ten_bo_mon')
			->get();
		return $array;	
	}
	public function lay_thong_tin_bo_mon2($tenbomon)
	{
		$array = DB::table('bo_mon')
			->where('bo_mon.ten_bo_mon','=',$tenbomon)
			->select('bo_mon.id')
			->first();
		return $array;	
	}
	public function lay_thong_tin_thong_bao1($idcanbo)
	{
		$array = DB::table('thong_bao')
			->join('bo_mon', function($join)
			{
				$join->on('bo_mon.id','=','thong_bao.id_bo_mon');
			})
			->join('can_bo', function($join)
			{
				$join->on('can_bo.id_nguoi_dung','=','thong_bao.id_nguoi_dung');
			})
			->select('bo_mon.ten_bo_mon','thong_bao.ngay_bat_dau','thong_bao.ngay_ket_thuc','thong_bao.nhom_nguoi_xem','thong_bao.noi_dung_tb', 'thong_bao.id')
			->where('can_bo.id','=',$idcanbo,'and')
			->where('thong_bao.hien_thi','=','1')
			->get();
		return $array;	
	}
	public function lay_id_quyen($motaquyen)
	{
		$array = DB::table('nhom_quyen_han')
			->select('nhom_quyen_han.id')
			->where('nhom_quyen_han.mo_ta_nhom_quyen','=',$motaquyen)
			->first();
		return $array;
	}
	public function lay_id_nguoi_dung($idcanbo)
	{
		$array = DB::table('can_bo')
			->select('can_bo.id_nguoi_dung')
			->where('can_bo.id','=',$idcanbo)
			->first();
		return $array;
	}
	public function lay_thong_tin_thong_bao2($idthongbao)
	{
		$array = DB::table('thong_bao')
			->select('thong_bao.noi_dung_tb','thong_bao.ngay_bat_dau','thong_bao.ngay_ket_thuc')
			->where('thong_bao.id','=',$idthongbao)
			->first();
		return $array;	
	}
	public function xoa_thong_bao()
	{
		$idthongbao = Input::get('txtIDThongBao');
		$array = DB::table('thong_bao')
			->where('thong_bao.id','=',$idthongbao)
			->update(array('hien_thi' => '0'));
		return Redirect::back()->with('ketquaxoa', $array);	
	}
	public function sua_thong_bao()
	{
		$idthongbao = Input::get('txtIDThongBao');
		$tenbomon = Input::get('txtTenBoMon');
		$idbomon = $this->lay_thong_tin_bo_mon2($tenbomon)->id;

		$idcanbo = Input::get('txtIDCanBo');
				
		$nguoixem1 = Input::get('txtNguoiXem1');
		$nguoixem2 = Input::get('txtNguoiXem2');
		$nguoixem3 = Input::get('txtNguoiXem3');
		$nguoixem4 = Input::get('txtNguoiXem4');
		$idquyen = "";
		if (isset($nguoixem1))
		{
			$idquyen1 = $this->lay_id_quyen($nguoixem1)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen1;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen1; 
			}  
		}
		if (isset($nguoixem2))
		{
			$idquyen2 = $this->lay_id_quyen($nguoixem2)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen2;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen2; 
			} 
		}
		if (isset($nguoixem3))
		{
			$idquyen3 = $this->lay_id_quyen($nguoixem3)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen3;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen3; 
			} 
		}
		if (isset($nguoixem4))
		{
			$idquyen4 = $this->lay_id_quyen($nguoixem4)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen4;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen4; 
			} 
		} 
		
		$noidungtb = Input::get('txtNoiDungTB');
		$ngaybatdau = Input::get('dtNgayBatDau');
		$ngayketthuc = Input::get('dtNgayKetThuc');
		$idthongbao = Input::get('txtIDThongBao');
		$array = DB::table('thong_bao')
			->where('thong_bao.id','=',$idthongbao)
			->update(array(
				'id_bo_mon' => $idbomon,
				'noi_dung_tb' => $noidungtb,
				'ngay_bat_dau' => $ngaybatdau,
				'ngay_ket_thuc' => $ngayketthuc,
				'nhom_nguoi_xem' => $idquyen));
		return Redirect::back()->with('ketquasua',$array);	
	}
	public function them_thong_bao()
	{
		$tenbomon = Input::get('txtTenBoMon');
		$idbomon = $this->lay_thong_tin_bo_mon2($tenbomon)->id;

		$idcanbo = Input::get('txtIDCanBo');
		$idnguoidung = $this->lay_id_nguoi_dung($idcanbo)->id_nguoi_dung;
		
		$nguoixem1 = Input::get('txtNguoiXem1');
		$nguoixem2 = Input::get('txtNguoiXem2');
		$nguoixem3 = Input::get('txtNguoiXem3');
		$nguoixem4 = Input::get('txtNguoiXem4');
		$idquyen = "";
		if (isset($nguoixem1))
		{
			$idquyen1 = $this->lay_id_quyen($nguoixem1)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen1;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen1; 
			}  
		}
		if (isset($nguoixem2))
		{
			$idquyen2 = $this->lay_id_quyen($nguoixem2)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen2;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen2; 
			} 
		}
		if (isset($nguoixem3))
		{
			$idquyen3 = $this->lay_id_quyen($nguoixem3)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen3;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen3; 
			} 
		}
		if (isset($nguoixem4))
		{
			$idquyen4 = $this->lay_id_quyen($nguoixem4)->id;
			if ($idquyen == "")
			{
				$idquyen = $idquyen4;
			}
			else
			{
				$idquyen = $idquyen.",".$idquyen4; 
			} 
		} 
			
		$noidungtb = Input::get('txtNoiDungTB');
		$ngaybatdau = Input::get('dtNgayBatDau');
		$ngayketthuc = Input::get('dtNgayKetThuc');
		$idthongbao = Input::get('txtIDThongBao');
		$array = DB::table('thong_bao')
			->insert(array(
				'id_bo_mon' => $idbomon,
				'id_nguoi_dung' => $idnguoidung,
				'noi_dung_tb' => $noidungtb,
				'ngay_bat_dau' => $ngaybatdau,
				'ngay_ket_thuc' => $ngayketthuc,
				'nhom_nguoi_xem' => $idquyen));
		return Redirect::back()->with('ketquathem',$array);	
	}
}
